package com.ternnetwork.oa.controller.note;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;

import com.ternnetwork.oa.dto.note.NoteDto;
import com.ternnetwork.oa.dto.note.NoteQueryDto;
import com.ternnetwork.oa.service.note.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/note/*")
public class NoteController {

    @Autowired
    private NoteService noteService;


    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(String requestJson, Principal user) throws Exception {
        NoteDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoteDto.class);
        return noteService.add(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(String requestJson, Principal user) throws Exception {
        NoteDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoteDto.class);
        return noteService.update(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id, Principal user) {
        return noteService.deleteById(id,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_FIND_BY_PAGE')")
    @RequestMapping("/findByPage")
    public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable, NoteQueryDto dto, Principal user) {
        return noteService.findByPage(pageable, dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_GET_ONE')")
    @RequestMapping("/getOne")
    public @ResponseBody BaseResponse getOne(Long id, Principal user) {
        return noteService.getOne(id,user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_SHARE_ADD')")
    @RequestMapping("/share/add")
    public @ResponseBody BaseResponse share(Long id, Principal user) {
        return noteService.addShare(id,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_SHARE_DELETE')")
    @RequestMapping("/share/delete")
    public @ResponseBody BaseResponse deleteShare(Long id, Principal user) {
        return noteService.deleteShare(id,user.getName());
    }

    @RequestMapping("/share/getOne")
    public @ResponseBody BaseResponse getShareOne(String shareCode) {
        return noteService.getShareOne(shareCode);
    }



}
